package programmercarl.贪心.C20;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

class Solution {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
        List<int[]> list = new ArrayList<>();
        int[] tmp = intervals[0];
        for (int i = 1; i < intervals.length ; i++) {
            if (intervals[i][0] <= tmp[1]) {
                tmp[1] = Math.max(tmp[1], intervals[i][1]);
            }else {
                list.add(tmp);
                tmp = intervals[i];
            }
        }
        list.add(tmp);

        return list.toArray(new int[list.size()][]);
    }
}